<?php

namespace IDock\Insurance\HBIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\HBIC\HBICEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends CommonResponse
{

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();
        $body = $this->getBody()['BODY'] ?? [];
        if ($body) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }
        $itemInfo = $body['itemInfo'] ?? [];
        //车牌号
        $renewalInquiryPrinter->licenseNo = $itemInfo['plateNum'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $itemInfo['frameNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $itemInfo['engineNum'] ?? '';
        $applicantPrinter = new PremiumRelationPrinter();
        $insuredPrinter = new PremiumRelationPrinter();
        $ownerPrinter = new PremiumRelationPrinter();
        $vehiclePrinter = new VehiclePrinter();

        $ownerPrinter->name = $itemInfo['carOwner'] ?? '';

        $vehiclePrinter->vehicleYear = $itemInfo['enrollDate'] ?? '';
        $vehiclePrinter->brandName = $itemInfo['brandName'] ?? '';
        $vehiclePrinter->vehicleModelCode = $itemInfo['modelCode'] ?? '';
        $vehiclePrinter->vehicleSeat = $itemInfo['seatCount'] ?? '';
        $vehiclePrinter->vehicleTonnage = $itemInfo['tonCount'] ?? '';
        $vehiclePrinter->vehicleExhaust = $itemInfo['exhaustScale'] ?? '';
        $vehiclePrinter->power = $itemInfo['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $itemInfo['purchasePrice'] ?? '';

        //保险信息
        $renewalInquiryPrinter->ciStartData = $body['forceRiskInfo']['startDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $body['forceRiskInfo']['endDate'] ?? '';
        $renewalInquiryPrinter->ciPolicyNo = $body['baseInfo']['efcPolicyCode'] ?? '';

        $renewalInquiryPrinter->biStartData = $body['businessRiskInfo']['startDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $body['businessRiskInfo']['endDate'] ?? '';
        $renewalInquiryPrinter->biPolicyNo = $body['baseInfo']['bizPolicyCode'] ?? '';

        $coverageList = $body['businessRiskInfo']['itemKindList']['itemKindInfo'] ?? [];
        foreach ($coverageList as $val) {
            $coveragePrinter = new PremiumCoveragePrinter();
            $coveragePrinter->coverageCode = array_search($val['kindCode'] ?? '', HBICEnums::$coverageCode);
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = (string)round($val['sumPremium'] ?? '', 2) ?? '';
            $coveragePrinter->unitAmount = (string)round($val['unitInsured'] ?? '', 2) ?? '';
            $coveragePrinter->quantity = $val['quantity'] ?? '';

            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $renewalInquiryPrinter->vehiclePrinter = $vehiclePrinter;
        $renewalInquiryPrinter->ownerPrinter = $ownerPrinter;
        $renewalInquiryPrinter->applicantPrinter = $applicantPrinter;
        $renewalInquiryPrinter->insuredPrinter = $insuredPrinter;

        $this->printer = $renewalInquiryPrinter;
        return $this;
    }
}